package com.gc.tpas.controller;

import com.gc.tpas.entity.QaUser;
import com.gc.tpas.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2018/4/27 0027.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public String login(String name, String pwd, HttpSession session){
        QaUser user = userService.login(name, pwd);
        if(user!=null){
            if(user.getStatus()==1){
                session.setAttribute("user",user);
                return "success";
            }else{
                return "lock";
            }
        }
        return "error";
    }

    @RequestMapping("/register")
    @ResponseBody
    public String register(QaUser user){
        try {
            user = userService.register(user);
            if(user!=null){
                return "success";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }

    @RequestMapping("/showall")
    public String showAll(Model model){
        model.addAttribute("ulist",userService.getAllUser());
        return "show";
    }



}
